import httpFetch from "share/httpFetch";
import config from "config";
import { formatNumber, messages } from "share/common";
import {
  commonPieReportFactory,
  commonBarReportFactory,
  approveEfficiencyTendencyReportFactory
} from "../echarts-data-transfer";
import { lengthStrShow } from "../../report-utils";
import errorMessage from "share/errorMessage";

export default {
  // 获取概览区数据
  getReportOverview(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/approval/efficiency/detail/total', params)
        .then((response) => {
          let { approvalTotalNumber, nodeAvgTime, formAvgTime, formAvgNodeNumber } = response.data || {};
          let dataLength = 1;
          let options = [{
            title: messages('finance-7.key176')/*总操作数*/,
            detail: approvalTotalNumber ? formatNumber(approvalTotalNumber) : 0
          }, {
            title: messages('finance-7.key177')/*节点平均时长*/,
            detail: nodeAvgTime ? formatNumber(nodeAvgTime) : 0
          }, {
            title: messages('finance-7.key178')/*表单平均时长*/,
            detail: formAvgTime ? formatNumber(formAvgTime) : 0
          }, {
            title: messages('finance-7.key179')/*表单平均节点数*/,
            detail: formAvgNodeNumber ? formatNumber(formAvgNodeNumber) : 0
          }];
          resolve({
            dataLength,
            options
          });
        })
    })
  },

  // 获取数据概览区-表单名称环状图
  getReportFormNameTotal(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/approval/efficiency/formName/total', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonPieReportFactory(data, true);
          resolve(res);
        })
    })
  },

  // 获取数据概览区-主数据区-月份与操作数
  getTendencyReport(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/approval/efficiency/month/total', params)
        .then((response) => {
          let data = response.data || [];
          let dataLength = data.length;
          let options;
          options = approveEfficiencyTendencyReportFactory(data);
          resolve({
            dataLength,
            options
          });
        })
    })
  },

  // 获取数据概览区-节点平均时长
  getNodeTime(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/approval/efficiency/node/time', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonBarReportFactory(data, 0, 'label', true);
          res.options.series[0].name = messages('finance-7.key314')/*时长(h)*/;
          res.options.legend.data = [messages('finance-7.key314')/*时长(h)*/];
          resolve(res);
        })
    })
  },
  // 获取数据概览区-表单平均时长
  getFormAvgTime(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/approval/efficiency/formName/time', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonBarReportFactory(data, 0, 'label', true);
          res.options.series[0].name = messages('finance-7.key314')/*时长(h)*/;
          res.options.legend.data = [messages('finance-7.key314')/*时长(h)*/];
          resolve(res);
        })
    })
  },
  // 获取数据概览区-表单平均节点数
  getFormAvgNode(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/approval/efficiency/avg/node/count', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonBarReportFactory(data, 0, 'label', true);
          res.options.series[0].name = messages('finance-7.key315')/*节点数(个)*/;
          res.options.legend.data = [messages('finance-7.key315')/*节点数(个)*/];
          resolve(res);
        })
    })
  },

  // 明细数据查询
  getDetailList(params, pagination) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/search/approval/efficiency/detail/list' +
        '?page=' + pagination.page + "&size=" + pagination.size, params)
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  // 明细数据的title数据列
  getDetailListColunms() {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/report/api/report/getReport?id=30001')
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  }
}
